//
//  PEInputOutputHeaderView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/25.
//

#import "RHInputHeaderView.h"
#import "RHInputOutputItemCell.h"

@interface RHInputHeaderView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectView;


@end
@implementation RHInputHeaderView

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}

-(instancetype)init{
    if(self = [super init]){
        [self setupUI];
    }
    return self;
}

-(void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    NSNumber *power = [NSNumber numberWithFloat:[dic[@"power"] floatValue]];
    NSNumber *todayGeneration = [NSNumber numberWithFloat:[dic[@"todayGeneration"] floatValue]];
    NSNumber *totalGeneration = [NSNumber numberWithFloat:[dic[@"accumulatedGeneration"] floatValue]];
    
    self.itemArr = @[@{CellTitle:@"功率",CellContent:[NSString stringWithFormat:@"%@kw",[power decimalPlacesWithoutRoundDownWithDigit:1]],CellIcon:@"dev_input_power",@"color":@"ffffff"},
                          @{CellTitle:@"今日发电",CellContent:[NSString stringWithFormat:@"%@kwh",[todayGeneration decimalPlacesWithoutRoundDownWithDigit:1]],CellIcon:@"dev_input_today_charge",@"color":@"ffffff"},
                          @{CellTitle:@"累计发电",CellContent:[NSString stringWithFormat:@"%@kwh",[totalGeneration decimalPlacesWithoutRoundDownWithDigit:1]],CellIcon:@"dev_input_total_charge",@"color":@"ffffff"}
                          ];
    
}
-(void)setItemArr:(NSArray *)itemArr
{
    _itemArr = itemArr;
    [self.collectView reloadData];
}

-(void)setupUI
{
    self.itemArr = @[@{CellTitle:@"功率",CellContent:@"--",CellIcon:@"dev_input_power",@"color":@"ffffff"},
                          @{CellTitle:@"今日发电",CellContent:@"--",CellIcon:@"dev_input_today_charge",@"color":@"ffffff"},
                          @{CellTitle:@"累计发电",CellContent:@"--",CellIcon:@"dev_input_total_charge",@"color":@"ffffff"}
                          ];
    
    UIView *contentView = kView;
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectView.backgroundColor = CFTabViewBgColor;
    _collectView.showsVerticalScrollIndicator = NO;
    _collectView.showsHorizontalScrollIndicator = NO;
    _collectView.delegate = self;
    _collectView.dataSource = self;
    [_collectView registerClass:[RHInputOutputItemCell class] forCellWithReuseIdentifier:@"RHInputOutputItemCell"];
    [contentView addSubview:_collectView];
    
    
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(kScaleW(80)*2+15);
        make.bottom.top.mas_offset(0);
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHInputOutputItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHInputOutputItemCell" forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:self.itemArr[indexPath.item][CellIcon]];
    cell.titleLab.text = [NSString stringWithFormat:@"%@%@",self.itemArr[indexPath.item][CellTitle],self.itemArr[indexPath.item][CellUint]];
    cell.contentLab.text = self.itemArr[indexPath.item][CellContent];
    cell.bgView.backgroundColor = [UIColor whiteColor];
    return  cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kUI_WINDOW_W -15*3)/2, kScaleW(70));
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

@end
